package com.example.controller;


import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.annotation.Log;
import com.example.common.result.Result;
import com.example.mapper.MoctcMapper;
import com.example.mapper.MocteMapper;
import com.example.service.MoctcService;
import com.example.service.impl.ActFlowCommService;
import com.example.untils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lan
 * @since 2024-07-30
 */
@RestController
@RequestMapping("/api/moctc")
@Api(tags = "材料(领,补,退)单前端控制台")
@Slf4j
public class MoctcController {

    @Resource
    private ActFlowCommService actFlowCommService;

    @Resource
    private MoctcService moctcService;

    @Resource
    private MoctcMapper moctcMapper;

    @Resource
    private MocteMapper mocteMapper;

    @Log("上报材料领料单")
    @ApiOperation("上报材料领料单")
    @GetMapping("/report")
    @DSTransactional
    public Result report(
            @RequestParam String TC001, @RequestParam String TC002, @RequestParam String username, @RequestParam String leader
    ) {
        if (StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        if (StringUtils.isEmpty(username)) {
            return Result.error("用户名为空");
        }
        String formkey = "moctc";
        String beanName = formkey + "ServiceImpl";
        ProcessInstance processInstance;
        String bussinessKey = formkey + ":" + TC001 + ":" + TC002;
        String id = TC001 + ":" + TC002;
        Boolean b = actFlowCommService.isOnly(bussinessKey, formkey);
        Boolean b2 = moctcService.tc30IsN(TC001, TC002);
        if (b && b2) {
            processInstance = actFlowCommService.startProcess(formkey, beanName, bussinessKey, id, username, leader);
        } else {
            return Result.error("有相同单据正在审核或该单据已审核完成");
        }
        //获取流程实例id
        String processInstanceId = processInstance.getId();
        List<Task> taskList = actFlowCommService.myTaskList(username);
        if (!CollectionUtils.isEmpty(taskList)) {
            for (Task task : taskList) {
                if (task.getAssignee().equals(username) &&
                        task.getProcessInstanceId().equals(processInstanceId)) {
                    log.info("ProcessInstanceId is {}", task.getProcessInstanceId());
                    log.info("taskid is {}",task.getId());
                    actFlowCommService.completeProcess("同意", username, task.getId(),"");
                }
            }
        }
        else {
            return Result.error("空任务！");
        }
        return Result.success("上报材料领料单成功");
    }


    @Log("上报材料补料单")
    @ApiOperation("上报材料补料单")
    @GetMapping("/reportBl")
    @DSTransactional
    public Result reportBl(
            @RequestParam String TC001, @RequestParam String TC002, @RequestParam String username, @RequestParam String leader
    ) {
        if (StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        if (StringUtils.isEmpty(username)) {
            return Result.error("用户名为空");
        }
        String formkey = "moctcbl";
        String beanName = formkey + "ServiceImpl";
        ProcessInstance processInstance;
        String bussinessKey = formkey + ":" + TC001 + ":" + TC002;
        String id = TC001 + ":" + TC002;
        Boolean b = actFlowCommService.isOnly(bussinessKey, formkey);
        Boolean b2 = moctcService.tc30IsN(TC001, TC002);
        if (b && b2) {
            processInstance = actFlowCommService.startProcess(formkey, beanName, bussinessKey, id, username, leader);
        } else {
            return Result.error("有相同单据正在审核或该单据已审核完成");
        }
        //获取流程实例id
        String processInstanceId = processInstance.getId();
        List<Task> taskList = actFlowCommService.myTaskList(username);
        if (!CollectionUtils.isEmpty(taskList)) {
            for (Task task : taskList) {
                if (task.getAssignee().equals(username) &&
                        task.getProcessInstanceId().equals(processInstanceId)) {
                    log.info("ProcessInstanceId is {}", task.getProcessInstanceId());
                    log.info("taskid is {}",task.getId());
                    actFlowCommService.completeProcess("同意", username, task.getId(),"");
                }
            }
        }
        else {
            return Result.error("空任务！");
        }
        return Result.success("上报材料补料单成功");
    }


    @Log("上报材料退料单")
    @ApiOperation("上报材料退料单")
    @GetMapping("/reportTl")
    @DSTransactional
    public Result reportTl(
            @RequestParam String TC001, @RequestParam String TC002, @RequestParam String username, @RequestParam String leader
    ) {
        if (StringUtils.isEmpty(TC001) || StringUtils.isEmpty(TC002)) {
            return Result.error("单别或单号为空");
        }
        if (StringUtils.isEmpty(username)) {
            return Result.error("用户名为空");
        }
        String formkey = "moctc";
        String beanName = formkey + "ServiceImpl";
        ProcessInstance processInstance;
        String bussinessKey = formkey + ":" + TC001 + ":" + TC002;
        String id = TC001 + ":" + TC002;
        Boolean b = actFlowCommService.isOnly(bussinessKey, formkey);
        Boolean b2 = moctcService.tc30IsN(TC001, TC002);
        if (b && b2) {
            processInstance = actFlowCommService.startProcess(formkey, beanName, bussinessKey, id, username, leader);
        } else {
            return Result.error("有相同单据正在审核或该单据已审核完成");
        }
        //获取流程实例id
        String processInstanceId = processInstance.getId();
        List<Task> taskList = actFlowCommService.myTaskList(username);
        if (!CollectionUtils.isEmpty(taskList)) {
            for (Task task : taskList) {
                if (task.getAssignee().equals(username) &&
                        task.getProcessInstanceId().equals(processInstanceId)) {
                    log.info("ProcessInstanceId is {}", task.getProcessInstanceId());
                    log.info("taskid is {}",task.getId());
                    actFlowCommService.completeProcess("同意", username, task.getId(),"");
                }
            }
        }
        else {
            return Result.error("空任务！");
        }
        return Result.success("上报材料退料单成功");
    }

}
